package com.landking.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.landking.R;
import com.landking.base.BaseMainFragment;
import com.landking.http.HttpValues;
import com.landking.test_bmob.BmobActivity;
import com.landking.utils.PrefUtils;

import java.lang.ref.SoftReference;


/**
 *
 */
public class MineFragment extends BaseMainFragment implements View.OnClickListener {

    /**
     * UI
     */
    private View mContentView;
    private FrameLayout containerFl;
    private RelativeLayout backBtn;
    private RelativeLayout confirmBtn;
    private AppCompatTextView confirmTv;
    private AppCompatTextView backTv;
    private AppCompatTextView titleTv;
    private ImageView logoImg;
    private Button settingipBtn;
    private EditText settingipEt;

    String title = "我的";// 需要改


    public static MineFragment newInstance() {
        Bundle args = new Bundle();
        MineFragment fragment = new MineFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static MineFragment newInstance(Bundle bundle) {
        Bundle args = bundle;
        MineFragment fragment = new MineFragment();
        fragment.setArguments(args);
        return fragment;
    }

    MyHandler handler;

    /**
     * 接收子线程消息的 Handler
     */
    public static class MyHandler extends Handler {

        // 软引用
        SoftReference<MineFragment> fragmentRef;

        public MyHandler(MineFragment fragment) {
            fragmentRef = new SoftReference<MineFragment>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            MineFragment fragment = fragmentRef.get();
            if (fragment == null) {
                return;
            }
            Bundle bundle = msg.getData();
            // 处理UI 变化
            switch (msg.what) {
                case 0:
                    //fragment.showAddProSuc(bundle);
                    break;
                case 1:
                    // fragment.showAdapter(bundle);
                    break;
                case 2:
                    // fragment.showTzAdapter(bundle);
                    break;
            }
        }
    }

    /**
     * data
     */
    public MineFragment() {
    }

    // 获取传递过来的参数
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            title = (String) arguments.get("title");// 需要改
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        // 不重复加载 做缓存处理
        if (mContentView == null) {
            mContentView = inflater.inflate(R.layout.fragment_mine_layout, container, false);// 需要改
            this.initView();
            this.initData();
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) mContentView.getParent();
        if (parent != null) {
            parent.removeView(mContentView);
        }
        return mContentView;
    }

    // 初始化视图
    private void initView() {
        containerFl = (FrameLayout) mContentView.findViewById(R.id.mine_container);// 需要改
        backBtn = (RelativeLayout) mContentView.findViewById(R.id.top_navigation_rl_back);
        confirmBtn = (RelativeLayout) mContentView.findViewById(R.id.top_navigation_rl_confirm);
        confirmTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_bt_confirm);
        backTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_bt_back);
        titleTv = (AppCompatTextView) mContentView.findViewById(R.id.top_navigation_tv_title);
        backBtn.setVisibility(View.INVISIBLE);
        confirmBtn.setVisibility(View.INVISIBLE);
        backBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
        titleTv.setOnClickListener(this);

        settingipEt = (EditText) mContentView.findViewById(R.id.mine_et_settingip);//
        settingipBtn = (Button) mContentView.findViewById(R.id.mine_bt_settingip);//
        logoImg = (ImageView) mContentView.findViewById(R.id.app_logo);//

        settingipBtn.setOnClickListener(this);
    }

    // 初始化数据
    public void initData() {
        titleTv.setText(R.string.app_name);// 需要改

        Glide.with(getActivity()).load(R.mipmap.ic_launcher).into(logoImg);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_navigation_tv_title://  // 需要改
                startActivity(new Intent(getActivity(), BmobActivity.class));
                break;
            case R.id.mine_bt_settingip://  // 需要改
                String ip = settingipEt.getText().toString();
                if (TextUtils.isEmpty(ip)) {
                    PrefUtils.putString(getActivity(), HttpValues.HTTP_IP, ip);
                } else {
                    Toast.makeText(getActivity(), "请先输入ip地址", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}